package com.tsxy.controller.administrator;

import com.tsxy.common.APIResult;
import com.tsxy.controller.dto.OrderNumber;
import com.tsxy.entity.BookSort;
import com.tsxy.service.BookSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/bookSort")
public class BookSortController {
    @Autowired
    private BookSortService bookSortService;

    @GetMapping("/getAll")
    public APIResult getAll(){
        List<BookSort> list = bookSortService.getAll();
        return APIResult.success(list);
    }

    /**
     * 获取所有分类每个月的销售总量
     */
    @GetMapping("/getNum")
    public  List<OrderNumber> getNum(){
        List<BookSort> list = bookSortService.getAll();
        HashMap<String, List<Integer>> rela = new HashMap<>();
        list.stream().forEach(bookSort -> {
            List<Integer> ids = new ArrayList<>();
            for (BookSort child : bookSort.getChildren()) {
                ids.add(child.getId());
            }
            rela.put(bookSort.getName(),ids);
        });
        Map<String, List<Integer>> num = bookSortService.getNum(rela);
        List<OrderNumber> s = new ArrayList<>();
        for (Map.Entry<String, List<Integer>> entry : rela.entrySet()) {
            OrderNumber number = new OrderNumber();
            if ( num.containsKey(entry.getKey()) ){
                number.setName(entry.getKey());
                number.setData(num.get(entry.getKey()));
            }else {
                number.setName(entry.getKey());
                ArrayList<Integer> list1 = new ArrayList<>();
                number.setData(list1);
            }
            s.add(number);
        }
        /*for (Map.Entry<String, List<Integer>> entry : num.entrySet()) {

            OrderNumber number = new OrderNumber();
            number.setName(entry.getKey());
            number.setData(entry.getValue());
            s.add(number);
        }*/
        return s;
    }
}
